//
//  PlanDetailVC.m
//  DefendHealth
//
//  Created by 吾诺瀚卓 on 17/2/25.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "PlanDetailVC.h"
#import "PlanDetailCell.h"
#import "FSCalendar.h"

@interface PlanDetailVC ()<FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>

{
    NSMutableArray * _detailArr;
//    NSInteger _selectTag;
    
    UIImageView * _seleImg;
    
}


@property (strong, nonatomic) NSDateFormatter *Todaydate;

@end

@implementation PlanDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    [self.rigltbutton setTitle:@"取消计划" forState:UIControlStateNormal];
    UIButton *releaseBtn = [[UIButton alloc] initWithFrame:CGRectMake(WindowWidth-10-[ToolUtil autoWidth:@"取消计划" width:500 height:13 num:13], 20, [ToolUtil autoWidth:@"取消计划" width:500 height:13 num:13], 44)];
    [releaseBtn setTitle:@"取消计划" forState:UIControlStateNormal];
//    [releaseBtn addTarget:self action:@selector(release:) forControlEvents:UIControlEventTouchUpInside];
    releaseBtn.titleLabel.font = FONT(13);
    releaseBtn.titleLabel.textAlignment = 2;
    [releaseBtn setTitleColor:[ImageUtil colorWithRgb:0x333333] forState:0];
    [self.view addSubview:releaseBtn];



    _detailArr = [[NSMutableArray alloc]initWithCapacity:0];
    NSArray * arr=@[@{@"title":@"慢走",@"status":@"完成",@"content":@"配速2.5|坡度1.0|5分钟"},@{@"title":@"慢跑",@"status":@"进行中",@"content":@"配速5.0|坡度2.0|5分钟"},@{@"title":@"高坡慢跑",@"status":@"未开始",@"content":@"配速4.0|坡度1.0|5分钟"},@{@"title":@"快跑",@"status":@"未开始",@"content":@"配速9.0|坡度1.0|5分钟"},];
    [_detailArr addObjectsFromArray:arr];
    
    self.baseTableView.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight-64);
    self.baseTableView.backgroundColor = k_TableBackColor;
    self.baseTableView.tableHeaderView = [self tableHeaderView];
    
    [self.view addSubview:self.leftbutton];
    [self.view addSubview:self.rigltbutton];
    
    
    
    self.Todaydate = [[NSDateFormatter alloc] init];
    self.Todaydate.dateFormat = @"yyyy-MM-dd";
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _detailArr.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30+13+8+10+11;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * cellIdentif = @"PlanDetailCell";
    PlanDetailCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentif];
    if (!cell) {
        cell = [[PlanDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PlanDetailCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellInfo:_detailArr[indexPath.row]];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView * )tableHeaderView{
   
    UIView * tableHeaderView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30+21+33+19+8+10+22.5+12+11+4+27.5+10+7+31+20+17+1+10+11)];
    tableHeaderView.backgroundColor = kWhiteColor;
    UILabel * title = [ReduceCode creatLabelWithFrame:CGRectMake(10, 30, kScreenWidth-20, 21) title:@"20天强力燃脂跑" titleColor:UIColorFromRGB(0x1c100d) font:21 textAlignment:NSTextAlignmentLeft];
    title.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:21];
    [tableHeaderView addSubview:title];
//    title.transform = CGAffineTransformScale(title.transform, 0.9, 1.5);
    NSArray * contentArr = @[@"6.0",@"3250",@"52"];
    NSArray * titleArr = @[@"里程/公里",@"消耗/千卡",@"耗时/分钟"];
    for (int i = 0 ; i < contentArr.count ; i ++ ) {
        UILabel * contentLab = [ReduceCode creatLabelWithFrame:CGRectMake(10+(kScreenWidth-20)/3*i, kMaxY(title.frame)+33, (kScreenWidth-20)/3, 19) title:contentArr[i] titleColor:UIColorFromRGB(0x231815) font:19 textAlignment:NSTextAlignmentLeft];
        contentLab.transform = CGAffineTransformScale(title.transform, 1, 1.2);
        contentLab.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:19];
        [tableHeaderView addSubview:contentLab];
        
        UILabel * titleLab = [ReduceCode creatLabelWithFrame:CGRectMake(10+(kScreenWidth-20)/3*i, kMaxY(contentLab.frame)+8, (kScreenWidth-20)/3, 19) title:titleArr[i] titleColor:UIColorFromRGB(0x231815) font:10 textAlignment:NSTextAlignmentLeft];
        [tableHeaderView addSubview:titleLab];
    }
    UILabel * timeLab = [ReduceCode creatLabelWithFrame:CGRectMake(10, kMaxY(title.frame)+33+19+8+10+22.5, (kScreenWidth-20)/2, 12) title:@"第1/20天" titleColor:kBlackColor font:12 textAlignment:NSTextAlignmentLeft];
    [tableHeaderView addSubview:timeLab];
    UILabel * scheduleLab = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth-10-(kScreenWidth-20)/2, kMaxY(title.frame)+33+19+8+10+22.5, (kScreenWidth-20)/2, 12) title:@"完成40%" titleColor:kBlackColor font:12 textAlignment:NSTextAlignmentRight];
    [tableHeaderView addSubview:scheduleLab];
    
    UILabel * blackLine = [[UILabel alloc]initWithFrame:CGRectMake(10, kMaxY(timeLab.frame)+11, (kScreenWidth-20)*0.4, 4)];
    blackLine.backgroundColor = UIColorFromRGB(0x313333);
    [tableHeaderView addSubview:blackLine];
    UILabel * grayLine = [[UILabel alloc]initWithFrame:CGRectMake(kMaxX(blackLine.frame), kMaxY(timeLab.frame)+11, (kScreenWidth-20)*0.6, 4)];
    grayLine.backgroundColor = UIColorFromRGB(0xebebea);
    [tableHeaderView addSubview:grayLine];
    
    
    FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(10, kMaxY(blackLine.frame)+20, kScreenWidth-20, 300)];
    calendar.scope = FSCalendarScopeWeek;
//    calendar.backgroundColor = kRedColor;
//   calendar.appearance.adjustsFontSizeToFitContentSize = NO; // 关闭文字尺寸自适应
    calendar.dataSource = self;
    calendar.delegate = self;
//    calendar.pagingEnabled = NO;
    //颜色
    calendar.appearance.headerTitleColor = UIColorFromRGB(0x666666);
    calendar.appearance.weekdayTextColor = UIColorFromRGB(0x666666);
    calendar.appearance.titleSelectionColor = kWhiteColor;//选中时字体的颜色;
    calendar.appearance.selectionColor = kBlackColor;//选择时的背景色;
    calendar.appearance.todayColor = kBlackColor;
    calendar.clipsToBounds = YES;
//     calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesDefaultCase
//    calendar.placeholderType = FSCalendarPlaceholderTypeNone;//是否显示别的月份
//    calendar.allowsMultipleSelection = YES;
    
    //大小
    calendar.headerHeight = 0.f;//标题大小
//    calendar.appearance.weekdayFont = [UIFont systemFontOfSize:12];
//    calendar.appearance.titleFont = [UIFont systemFontOfSize:12];
    
//    calendar.appearance.headerMinimumDissolvedAlpha = 0.f;
//    calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase;
    
    [tableHeaderView addSubview:calendar];
    
    [ToolUtil createLine:kMinY(calendar.frame)+80 superView:tableHeaderView lift:10 width:kScreenWidth-20 hight:1 color:kLineColor];
    
    UIImageView * jImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, kMinY(calendar.frame)+71, 20, 10)];
//    jImg.centerX = 10+(kScreenWidth-20)/14;
    jImg.backgroundColor = kWhiteColor;
    _seleImg = jImg;
    jImg.image = IMAGE(@"triangle");
    [tableHeaderView addSubview:jImg];
    
    UILabel * lab = [ReduceCode creatLabelWithFrame:CGRectMake(10, kMaxY(jImg.frame)+20, kScreenWidth-20, 12) title:@"流程详解" titleColor:UIColorFromRGB(0x231815) font:12 textAlignment:NSTextAlignmentLeft];
    lab.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:12];
    [tableHeaderView addSubview:lab];
    return tableHeaderView;
}




- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
    calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
    
    NSLog(@"sasa");
    
}

- (nullable UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance fillDefaultColorForDate:(NSDate *)date{
    
    if ([calendar isDateInToday:date]) {
        
        [self imageAnimation:[calendar weekdayOfDate:date]*2-1];//1,3,5,7,9....
        
      return kBlackColor;
    }
    return UIColorFromRGB(0xebebea);
}

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date{
    
    [self imageAnimation:[calendar weekdayOfDate:date]*2-1];//1,3,5,7,9....
  
}

- (void)imageAnimation:(NSInteger)inde {
   
    CGFloat calWeight = (kScreenWidth-20)/14;//获取平均值的一半
    
    [UIView animateWithDuration:0.3 animations:^{
        _seleImg.centerX = 10+calWeight*inde;
    }];
}

- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date
{
    if ([[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian] isDateInToday:date]) {
        return @"今";
    }
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
@end
